<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class HhmGoodsSpec extends Model
{
    use SoftDeletes;

    protected $table = 'ims_hhm_goods_spec';
    
    protected $fillable = [
        'uniacid', 'goods_id', 'title', 'description', 'display_type',
        'content', 'display_order', 'prop_id', 'is_required'
    ];

    protected $casts = [
        'content' => 'json',
        'is_required' => 'boolean',
    ];

    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';
    const DELETED_AT = 'deleted_at';

    // 禁用时间戳自动转换为Carbon对象，因为表中使用整型存储
    public $timestamps = true;
    protected $dateFormat = 'U'; // 使用Unix时间戳

    /**
     * 显示类型常量
     */
    const DISPLAY_TYPE_TEXT = 0;
    const DISPLAY_TYPE_IMAGE = 1;
    const DISPLAY_TYPE_COLOR = 2;

    /**
     * 关联商品
     */
    public function goods()
    {
        return $this->belongsTo(HhmGoods::class, 'goods_id', 'id');
    }

    /**
     * 关联规格项
     */
    public function items()
    {
        return $this->hasMany(HhmGoodsSpecItem::class, 'spec_id', 'id')
            ->where('show', 1)
            ->orderBy('display_order', 'asc');
    }
}

